<div class="well clearfix">
    <p class=" col-md-12 text-success">
        <?php
        echo '<kbd>' . $number_cmt . "</kbd> Comment";
        ?>
    </p>

    <div class="col-md-12 item-cmt">
        <?php foreach ($cmts as $cmt): ?>
            <blockquote>
                <p class="text-warning"><?php $cmt['Comment']['comment'] = preg_replace('/<br>/', '/n', $cmt['Comment']['comment']);
                    echo h($cmt['Comment']['comment']) ?></p>
                <footer><?php echo $cmt['Comment']['who']; ?></footer>
            </blockquote>
            <hr/>
        <?php endforeach; ?>
    </div>
    <hr/>
    <?php echo $this->Form->create('Comment', array('action' => 'add', 'class' => 'form-horizontal', 'inputDefaults' => array('label' => false))); ?>
    <div class="form-group">
        <label for="name" class="control-label col-md-2">Tên:</label>

        <div class="col-md-4">
            <?php
            if (isset($name)) {
                echo $this->Form->input('who', array('class' => 'form-control', 'value' => $name, 'placeholder' => 'Name...'));
            } else {
                echo $this->Form->input('who', array('class' => 'form-control', 'placeholder' => 'Name...'));
            }
            ?>
        </div>
    </div>
    <div class="form-group">
        <label for="name" class="control-label col-md-2">Nội dung:</label>

        <div class="col-md-9">
            <?php echo $this->Form->input('comment', array('class' => 'form-control', 'type' => 'textarea', 'rows' => 4, 'placeholder' => 'Comment...')); ?>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-2 col-md-offset-2">
            <?php  echo $this->Form->input('Submit', array('type' => 'button', 'class' => 'btn btn-info btn-xs col-md-2 form-control sm_comment')); ?>
            <span class="loadsm"><image width="24" height="24" src="<?php echo Router::url('/',true)?>img/load.gif"/></span>
            <span class="oksm"><image width="24" height="24" src="<?php echo Router::url('/',true)?>img/ok.png"/></span>
        </div>
    </div>
    <?php

    echo $this->Form->end();
    ?>

</div>
<script type="text/javascript">
    function validateTagHtml(text) {
        var pattern = /<(\"[^\"]*\"|'[^']*'|[^'\">])*>/;
        if (pattern.test(text)) {
            return true;
        }
        return false;
    }

    function validateTagScript(text) {
        var pattern = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;
        if (pattern.test(text)) {
            return true;
        }
        return false;
    }
    $('.item-order').on('click', '.sm_comment', function () {
        var parent_Comment = $(this).parent().parent().parent().parent().parent().parent().parent();
        var parent = $(this).parent().parent().parent().parent().parent().parent();
        var id = $(parent).attr('attr');
        var name = $('#CommentWho').val();
        var comment = $('#CommentComment').val();
        if (name.length == 0) {
            window.alert("Bạn chưa nhập tên!");
            $('#CommentWho').focus();
        } else if(validateTagHtml(name)||validateTagScript(name)) {
            alert('Tên bạn chứa ký tự không hợp lệ! Vui lòng kiểm tra lại');
            $("#CommentWho").focus();
        }
        else if (comment.length == 0) {
            window.alert("Bạn chưa nhập nội dung!");
            $('#CommentComment').focus();
        } else if(validateTagScript(comment)) {
            window.alert("Nội dung comment chứa ký tự không hợp lệ! ");
            $('#CommentComment').focus();
        } else {
            var url = "<?php echo $this->Html->url(array('controller'=>'comments','action'=>'add')) ?>";
            $('.loadsm').fadeIn();
            $.post(url, {orders_id: id, comment: comment, who: name}, function (res) {
                if (res == 'ok') {
                    $('.loadsm').fadeOut();
                    $('.oksm').fadeIn(1000).fadeOut();
                    var item = "<blockquote>";
                    item += "<p class='text-warning'>" + $.trim(comment) + "</p>";
                    item += "<footer>" + $.trim(name) + "</footer></blockquote>";

                    $(".item-cmt").append(item);
                    var number = parseInt($('p kbd').text()) + 1;
                    $('p kbd').text(number);
                    $(parent_Comment).find('.badge').text(number);
                    $('#CommentComment').val('');
					$('#CommentWho').val($.trim(name));
                } else
                    window.alert("Comment loi!");
            });
        }
        return false;
    });
</script>